﻿package {
	import flash.display.MovieClip;
	import flash.ui.Keyboard;
	import flash.events.*;
	import GameManager;
	import bullet;
	import Level;

	public class Character extends MovieClip {

		//Keys
		static public  const LEFT:int = Keyboard.LEFT;
		static public  const RIGHT:int = Keyboard.RIGHT;
		static public  const DOWN:int = Keyboard.DOWN;
		static public  const UP:int = Keyboard.UP;
		//wk5 fire button
		static public  const FIRE:int = Keyboard.SPACE;


		//Movement Properties
		private var _speed:int = 3;
		private var _leftPressed:Boolean = false;
		private var _rightPressed:Boolean = false;
		private var _upPressed:Boolean = false;
		private var _downPressed:Boolean = false;

		public var _NextX:int;
		public var _NextY:int;

		private var _GM:GameManager;

		public var isFacing:String;

		public function Character() {
			//listen for when the character is added to the play area
			addEventListener(Event.ADDED_TO_STAGE, onStage);
		}
		private function onStage(e:Event) {
			//remove the added listener
			removeEventListener(Event.ADDED_TO_STAGE, onStage);

			_GM = parent as GameManager;

			// add keyboard listeners
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKDown,false,0,true);
			stage.addEventListener(KeyboardEvent.KEY_UP,onKUp,false,0,true);

			//register listeners for update stuff if we decouple the system more

			//when removed from stage clean up after your self
			addEventListener(Event.REMOVED_FROM_STAGE, cleanUp);
		}
		
		private function cleanUp(e:Event) {
			removeEventListener(Event.REMOVED_FROM_STAGE, cleanUp);
			//Remove Keyboard listeners
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKDown);
			stage.removeEventListener(KeyboardEvent.KEY_UP,onKUp);
		}
		//START Input Management
		private function onKDown(e:KeyboardEvent):void {
			switch (e.keyCode) {
				case LEFT :
					_leftPressed = true;
					isFacing = "left";
					break;
				case RIGHT :
					_rightPressed = true;
					isFacing = "right";
					break;
				case DOWN :
					_downPressed = true;
					isFacing = "down";
					break;
				case UP :
					_upPressed = true;
					isFacing = "up";
					break;
				case FIRE :
					shootBullet();
					break;
				case 84:
					teleport();
					break;
			}
		}
		
		private function onKUp(e:KeyboardEvent):void {
			switch (e.keyCode) {
				case LEFT :
					gotoAndPlay("start");
					_leftPressed = false;
					//isFacing = "";
					break;
				case RIGHT :
					gotoAndPlay("start");
					_rightPressed = false;
					//isFacing = "";
					break;
				case DOWN :
					_downPressed = false;
					//isFacing = "";
					break;
				case UP :
					_upPressed = false;
					//isFacing = "";
					break;

			}
		}
		//changed from handleKeyInput
		public function nextPosition() {
			// set the _next position to the current position
			_NextX = x;
			_NextY = y;

			if (_leftPressed) {
				_NextX = x - _speed;
				nextFrame();
				if (currentLabel != "left") {
					gotoAndStop("left");
				}

			}
			if (_rightPressed) {
				_NextX = x + _speed;

				nextFrame();
				if (currentLabel != "right") {
					gotoAndStop("right");
				}
			}
			if (_upPressed) {
				_NextY = y - _speed;
			}
			if (_downPressed) {
				_NextY = y + _speed;
			}
		}
		//END Input Management
		
		// cheat code
		public function teleport(){
			x = 480;
			y = 320;			
		}
		
		public function looseALife(){
			// loose a life and restart level
			_GM.lives--;
			_GM.startLevel();
		}
		public function checkCharacterCollision() {
			if (x<0 && y<0) {
				_NextX = _GM.levelMC.CharacterStart.x;
				_NextY = _GM.levelMC.CharacterStart.y;
			}
			// Bottom Left - check clip 
			if (_GM.levelMC.getChildByName("mazeClip").hitTestPoint(_NextX+11,_NextY+28,true)) {
				// if the next move position would hit 
				// would it if there was no x movement 
				if (_GM.levelMC.getChildByName("mazeClip").hitTestPoint(x+11,_NextY+28,true)) {
					_NextX = x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if (_GM.levelMC.getChildByName("mazeClip").hitTestPoint(_NextX+11,y+28,true)) {
					_NextY = y;
				}
			}
			// Bottom Right
			if (_GM.levelMC.getChildByName("mazeClip").hitTestPoint(_NextX+24,_NextY+28,true)) {
				// if the next move position would hit 
				// would it if there was no x movement 
				if (_GM.levelMC.getChildByName("mazeClip").hitTestPoint(x+24,_NextY+28,true)) {
					_NextX = x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if (_GM.levelMC.getChildByName("mazeClip").hitTestPoint(_NextX+24,y+28,true)) {
					_NextY = y;
				}

			}
			// Top Left
			if (_GM.levelMC.getChildByName("mazeClip").hitTestPoint(_NextX+11,_NextY+4,true)) {
				// if the next move position would hit 
				// would it if there was no x movement 
				if (_GM.levelMC.getChildByName("mazeClip").hitTestPoint(x+11,_NextY+4,true)) {
					_NextX = x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if (_GM.levelMC.getChildByName("mazeClip").hitTestPoint(_NextX+11,y+4,true)) {
					_NextY = y;
				}
			}
			// Top Right
			if (_GM.levelMC.getChildByName("mazeClip").hitTestPoint(_NextX+24,_NextY+4,true)) {
				// if the next move position would hit 
				// would it if there was no x movement 
				if (_GM.levelMC.getChildByName("mazeClip").hitTestPoint(x+24,_NextY+4,true)) {
					_NextX = x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if (_GM.levelMC.getChildByName("mazeClip").hitTestPoint(_NextX+24,y+4,true)) {
					_NextY = y;
				}
			}
			// check the fires
			// Bottom Left
			if (_GM.levelMC.getChildByName("firesClip").hitTestPoint(_NextX+11,_NextY+28,true)) {
				looseALife();
				//ADDED WK6
				return;
			}
			// Bottom Right
			if (_GM.levelMC.getChildByName("firesClip").hitTestPoint(_NextX+24,_NextY+28,true)) {
				looseALife();
				//ADDED WK6
				return;
			}
			// Top Left
			if (_GM.levelMC.getChildByName("firesClip").hitTestPoint(_NextX+11,_NextY+4,true)) {
				looseALife();				
				//ADDED WK6
				return;
			}
			// Top Right
			if (_GM.levelMC.getChildByName("firesClip").hitTestPoint(_NextX+24,_NextY+4,true)) {
				looseALife();
				//ADDED WK6
				return;
			}
			// check exit
			// Bottom Left
			if (_GM.levelMC.getChildByName("exitClip").hitTestPoint(_NextX+11,_NextY+28,true)) {
				//changed week 06 from exitGame
				_GM.exitLevel();
				//ADDED WK6
				return;
			}
			//Bottom Right
			if (_GM.levelMC.getChildByName("exitClip").hitTestPoint(_NextX+24,_NextY+28,true)) {
				_GM.exitLevel();
				//ADDED WK6
				return;
			}
			// Top Left
			if (_GM.levelMC.getChildByName("exitClip").hitTestPoint(_NextX+11,_NextY+4,true)) {
				_GM.exitLevel();
				//ADDED WK6
				return;
			}
			// Top Right
			if (_GM.levelMC.getChildByName("exitClip").hitTestPoint(_NextX+24,_NextY+4,true)) {
				_GM.exitLevel();
				//ADDED WK6
				return;
			}

			//wk5
			// check the guards
			for each (var guard in _GM.levelMC.guards) {
				// Bottom Left
				if (guard.hitTestPoint(_NextX+11,_NextY+28,true)) {
					looseALife();
					//ADDED WK6
					return;
				}
				// Bottom Right
				if (guard.hitTestPoint(_NextX+24,_NextY+28,true)) {
					looseALife();
					//ADDED WK6
					return;
				}
				// Top Left
				if (guard.hitTestPoint(_NextX+11,_NextY+4,true)) {
					looseALife();
					//ADDED WK6
					return;
				}
				// Top Right
				if (guard.hitTestPoint(_NextX+24,_NextY+4,true)) {
					looseALife();
					//ADDED WK6
					return;
				}
			}
		}
		//
		public function makeMove() {
			if (_NextX>=0 && _NextX<=500) {
				x = _NextX;
			}
			if (_NextY>=0 && _NextY<=350) {
				y = _NextY;
			}
		}
		private function shootBullet() {
			var bulletDirection:String = "";

			if (_GM.bulletRef==null) {
				bulletDirection = isFacing;
				//if the character is facing in a direction 
				if (bulletDirection!="") {
					//add bullet to the stage and register it in the bullet variable
					var aBullet:bullet = new bullet(bulletDirection,_GM.BULLETPOWER);
					_GM.bulletRef = aBullet;
					_GM.addChild(_GM.bulletRef);
					_GM.bulletRef.x = x + 16;
					_GM.bulletRef.y = y + 16;
				}
			}
		}

	}
}